﻿using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using Skybrud.Social.Vimeo.Objects;
using Skybrud.Social.Vimeo.Responses;

namespace Skybrud.Social.Vimeo {

    public class VimeoService {

        #region user channels

        public string GetUserChannelsAsRawXml(string name) {
            return SocialUtils.DoHttpGetRequestAndGetBodyAsString("http://vimeo.com/api/v2/" + name + "/channels.xml");
        }

        public VimeoChannelsResponse GetUserChannels(string name) {
            HttpWebResponse response = SocialUtils.DoHttpGetRequest("http://vimeo.com/api/v2/" + name + "/channels.xml");
            if (response.StatusCode == HttpStatusCode.OK) return VimeoChannelsResponse.Parse(XElement.Parse(response.GetAsString()));
            throw new VimeoException(response.GetAsString());
        }

        #endregion

        #region Channel info

        public string GetChannelInfoAsRawXml(string name) {
            return SocialUtils.DoHttpGetRequestAndGetBodyAsString("http://vimeo.com/api/v2/channel/" + name + "/info.xml");
        }

        public VimeoChannel GetChannelInfo(int id) {
            return GetChannelInfo(id + "");
        }

        public VimeoChannel GetChannelInfo(string name) {
            HttpWebResponse response = SocialUtils.DoHttpGetRequest("http://vimeo.com/api/v2/channel/" + name + "/info.xml");
            if (response.StatusCode == HttpStatusCode.OK) return VimeoChannel.ParseMultiple(XElement.Parse(response.GetAsString())).FirstOrDefault();
            throw new VimeoException(response.GetAsString());
        }

        #endregion

        #region Channel videos

        public string GetChannelVideosAsRawXml(string name, int page) {
            NameValueCollection query = new NameValueCollection();
            if (page >= 0) query.Set("page", page + "");
            return SocialUtils.DoHttpGetRequestAndGetBodyAsString("http://vimeo.com/api/v2/channel/" + name + "/videos.xml", query);
        }

        public VimeoChannelVideosResponse GetChannelVideos(int id) {
            return GetChannelVideos(id + "", 0);
        }

        public VimeoChannelVideosResponse GetChannelVideos(int id, int limit) {
            return GetChannelVideos(id + "", limit);
        }

        public VimeoChannelVideosResponse GetChannelVideos(string name) {
            return GetChannelVideos(name, 0);
        }

        public VimeoChannelVideosResponse GetChannelVideos(string name, int page) {
            NameValueCollection query = new NameValueCollection();
            if (page >= 0) query.Set("page", page + "");
            HttpWebResponse response = SocialUtils.DoHttpGetRequest("http://vimeo.com/api/v2/channel/" + name + "/videos.xml", query);
            if (response.StatusCode == HttpStatusCode.OK) return VimeoChannelVideosResponse.Parse(XElement.Parse(response.GetAsString()));
            throw new VimeoException(response.GetAsString());
        }

        #endregion

    }

}
